﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ExtenDev.Web.Management
{
    public enum ResourceType
    {
        Library,
        CSS,
        JavaScript,
        LessCSS
    }

    public enum ResourceContentType
    {
        Inline,
        LocalUrl,
        Embedded,
        Remote
    }

    public class Resource
    {
        private IEnumerable<Resource> dependencies;

        public Resource(IEnumerable<Resource> dependencies = null)
        {
            this.dependencies = dependencies;
        }

        public void RegisterTemporaryDependency(Resource dependency)
        {
            if (dependency == null) throw new ArgumentNullException("dependency");

            List<Resource> tmpDeps = HttpContext.Current.Items["MicroResource/" + this.Name + "/TemporaryDependencies"] as List<Resource>;
            if (tmpDeps == null)
            {
                tmpDeps = new List<Resource>();
                HttpContext.Current.Items["MicroResource/" + this.Name + "/TemporaryDependencies"] = tmpDeps;
            }
            else if (tmpDeps.Contains(dependency)) return;
            tmpDeps.Add(dependency);
        }

        public IEnumerable<Resource> Dependencies
        {
            get
            {
                if (this.dependencies != null)
                {
                    foreach (Resource res in this.dependencies)
                    {
                        yield return res;
                    }
                }
                List<Resource> tmpDeps = HttpContext.Current.Items["MicroResource/" + this.Name + "/TemporaryDependencies"] as List<Resource>;
                if (tmpDeps != null)
                {
                    foreach (Resource res in tmpDeps)
                    {
                        yield return res;
                    }
                }
            }
        }

        public ResourceType ContentType { get; set; }
        public ResourceContentType ReferenceType { get; set; }

        public string Name { get; set; }
        public string Content { get; set; }

        public override bool Equals(object obj)
        {
            return obj is Resource && ((Resource)obj).Name == Name;
        }

        public override int GetHashCode()
        {
            return Name == null ? 0 : Name.GetHashCode();
        }

    }
}
